<template>
  <div class="login_container">
    <!-- 登录模块 -->
    <div class="login_box">
      <!-- 头像框 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- form模块 -->
      <div class="login_form">
        <!-- from表单 -->
        <el-form
          :rules="loginFormRules"
          label-width="0"
          :model="loginFrom"
          ref="loginFormRef"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-user-solid"
              v-model="loginFrom.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              prefix-icon="el-icon-lock"
              v-model="loginFrom.password"
            ></el-input>
          </el-form-item>
          <!-- 登录重置 -->
          <div class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      // 绑定的登录表单数据
      loginFrom: {
        username: "admin",
        password: "123456",
      },
      // 登录表单的规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名：", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符" },
        ],
        password: [
          { required: true, message: "请输入登录密码：", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 重置表单信息
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 提交登录信息
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // 检测数据校验是否通过
        if (!valid) return;
        const result = await this.$http.post("login", this.loginFrom);
        if (result.data.meta.status !== 200)
          return this.$message.error("用户名或密码输入有误，请重新输入");
        this.$message.success("登录成功");
        // 获取token 保存到session
        const token = result.data.data.token;
        window.sessionStorage.setItem("token", token);
        // 登录成功后跳转到主页
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  position: relative;
  background: #2b4b6b;
  height: 100%;
}
// 登录框
.login_box {
  width: 450px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 6px;
  // 头像框
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #eee;
    background: #fff;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: #ccc;
    }
  }

  // 登录表单
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
  }

  //按钮
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>